package com.orgcent.activities;

import junit.framework.Test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 使用WebView打开网页
 */
public class WebViewHTML extends Activity {
	private static final String TAG = WebViewHTML.class.getSimpleName();
	private WebView content_wv;
	private WebSettings ws;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_PROGRESS);// 让进度条显示在标题栏上
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview_display_html);

		content_wv = (WebView) findViewById(R.id.content_wv);
		ws = content_wv.getSettings();
		ws.setJavaScriptEnabled(true); // 设置支持javascript脚本
		ws.setAllowFileAccess(true); // 允许访问文件
		ws.setBuiltInZoomControls(true); // 设置支持缩放

		content_wv.requestFocus();
		content_wv.setWebViewClient(new WebViewClientDemo());
		content_wv.setWebChromeClient(new WebViewChromeClientDemo());

		final String url = getIntent().getStringExtra(
				ListViewParseRssXml.CONTENT_URL);
		content_wv.loadUrl(url);
	}

	private class WebViewChromeClientDemo extends WebChromeClient {
		public boolean onJsAlert(WebView view, String url, String message,
				final JsResult result) {
			// 构建一个Builder来显示网页中的alert对话框
			Builder builder = new Builder(WebViewHTML.this);
			builder.setTitle("提示对话框");
			builder.setMessage(message);
			builder.setPositiveButton(android.R.string.ok,
					new AlertDialog.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							result.confirm();
						}

					});
			builder.setCancelable(false);
			builder.create();
			builder.show();
			return true;
		}

		public boolean onJsConfirm(WebView view, String url, String message,
				final JsResult result) {
			Builder builder = new Builder(WebViewHTML.this);
			builder.setTitle("带选择的对话框");
			builder.setMessage(message);
			builder.setPositiveButton(android.R.string.ok,
					new AlertDialog.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							result.confirm();
						}

					});
			builder.setNeutralButton(android.R.string.cancel,
					new AlertDialog.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							result.cancel();
						}

					});
			builder.setCancelable(false);
			builder.create();
			builder.show();
			return true;
		}

		public boolean onJsPrompt(WebView view, String url, String message,
				String defaultValue, final JsPromptResult result) {
			LayoutInflater inflater = LayoutInflater.from(WebViewHTML.this);
			final View v = inflater.inflate(R.layout.prom_dialog, null);
			// 设置 TextView对应网页中的提示信息
			((TextView) v.findViewById(R.id.TextView_PROM)).setText(message);
			// 设置EditText对应网页中的输入框
			((EditText) v.findViewById(R.id.EditText_PROM))
					.setText(defaultValue);
			Builder builder = new Builder(WebViewHTML.this);
			builder.setTitle("带输入的对话框 ");
			builder.setView(v);
			builder.setPositiveButton(android.R.string.ok,
					new AlertDialog.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							String value = ((EditText) v
									.findViewById(R.id.EditText_PROM))
									.getText().toString();
							result.confirm(value);
						}

					});
			builder.setNegativeButton(android.R.string.cancel,
					new AlertDialog.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							result.cancel();
						}

					});
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					result.cancel();
				}

			});
			builder.create();
			builder.show();
			return true;
		}

		// 设置网页加载的进度条
		public void onProgressChanged(WebView view, int newProgress) {
			WebViewHTML.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
					newProgress * 100); //在标题栏显示进度条
		}

		// 设置应用程序的标题
		public void onReceivedTitle(WebView view, String title) {
			WebViewHTML.this.setTitle(title);
		}
	}

	private class WebViewClientDemo extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);// 当有新连接时，使用当前的 WebView
			return true;
		}
	}

	@Override
	public boolean onKeyDown(int keyCoder, KeyEvent event) {
		if (keyCoder == KeyEvent.KEYCODE_BACK && content_wv.canGoBack()) {
			content_wv.goBack(); // goBack()表示返回webView的上一页面
		}
		return super.onKeyDown(keyCoder, event);
	}
}
